package com.draven.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.draven.mall.entity.Merchant;
import com.draven.mall.mapper.MerchantMapper;
import com.draven.mall.service.MerchantService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商家服务实现类
 */
@Service
public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant> implements MerchantService {

    @Override
    public Merchant login(String username, String password) {
        LambdaQueryWrapper<Merchant> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Merchant::getUsername, username)
               .eq(Merchant::getPassword, password);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public Merchant register(Merchant merchant) {
        // 检查用户名是否存在
        LambdaQueryWrapper<Merchant> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Merchant::getUsername, merchant.getUsername());
        long count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            return null;
        }
        
        // 设置默认值
        merchant.setStatus(1); // 默认为营业中
        
        // 插入数据
        baseMapper.insert(merchant);
        return merchant;
    }

    @Override
    public Map<String, Object> getNearbyShops(Double latitude, Double longitude, Long categoryId, 
                                            String keyword, Integer page, Integer size) {
        // 构建查询条件
        LambdaQueryWrapper<Merchant> wrapper = new LambdaQueryWrapper<>();
        
        // 只查询营业中的商家
        wrapper.eq(Merchant::getStatus, 1);
        
        // 根据分类ID筛选
        if (categoryId != null) {
            wrapper.eq(Merchant::getCategoryId, categoryId);
        }
        
        // 根据关键词搜索商家名称
        if (!StringUtils.isEmpty(keyword)) {
            wrapper.like(Merchant::getShopName, keyword);
        }
        
        // 实际项目中，这里应该根据经纬度计算距离并排序
        // 简化处理，直接分页查询
        Page<Merchant> pageParam = new Page<>(page, size);
        IPage<Merchant> pageModel = baseMapper.selectPage(pageParam, wrapper);
        
        // 构建返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("total", pageModel.getTotal());
        result.put("pages", pageModel.getPages());
        result.put("current", pageModel.getCurrent());
        result.put("size", pageModel.getSize());
        result.put("list", pageModel.getRecords());
        
        return result;
    }

    @Override
    public List<Merchant> getRecommendedShops() {
        // 在实际项目中，这里应该根据算法推荐商家
        // 简化处理，查询评分最高的5个商家
        LambdaQueryWrapper<Merchant> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Merchant::getStatus, 1)
               .orderByDesc(Merchant::getRating)
               .last("LIMIT 5");
        
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<Map<String, Object>> getAllMerchants() {
        // 查询所有已审核通过的商家
        LambdaQueryWrapper<Merchant> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Merchant::getStatus, 1);  // 状态为1表示已审核通过
                   
        // 执行查询
        List<Merchant> merchants = list(queryWrapper);
        
        // 转换为前端需要的格式
        List<Map<String, Object>> result = new ArrayList<>();
        for (Merchant merchant : merchants) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", merchant.getId());
            map.put("name", merchant.getShopName());
            map.put("address", merchant.getAddress());
            result.add(map);
        }
        
        return result;
    }
}
